﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.ServiceModel.Channels;
public partial class user_ShoppingCart : System.Web.UI.Page
{
    SqlConnection sqlcon;
    string strCon = "Server=.;Database=keshe2;Integrated Security=SSPI";
    private SqlDataAdapter da;
    //页面加载时被触发

    protected void Page_Load(object sender, EventArgs e)
    {
        Label5.Text = "0";
        Label3.Text = "0";
        if (!IsPostBack)
        {
            Label5.Text = "0";
            Label3.Text = "0";
            GridView1.DataBind();
            if (GridView1.Rows.Count == 0)
            {
                GridView1_empty();
                Button4.Visible = false;
                Label2.Visible = false;
                Label3.Visible = false;
                Label4.Visible = false;
                Label5.Visible = false;
                Label6.Visible = false;
                Label7.Visible = false;
                CheckBox2.Visible = false;
                Button1.Visible = false;
                Button2.Visible = false;
            }
            else
            {
                CheckBox2.Visible = true;
                Button1.Visible = true;
                Button2.Visible = true;
                Label2.Visible = true;
                Label3.Visible = true;
                Label4.Visible = true;
                Label5.Visible = true;
                Label6.Visible = true;
                Label7.Visible = true;

            }
        }

    }


    protected void dingdanAdd()
    {
        SqlConnection con = new SqlConnection(strCon);
        SqlConnection sqlcon = new SqlConnection(strCon);
        sqlcon.Open();
        con.Open();

        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");

            if (cbox.Checked == true)
            {
                //商品名称
                string Sname = GridView1.Rows[i].Cells[1].Text;
                //通过商品名称转换成商品编号
                string sqlstr2 = "select 商品.商品编号  from 商品 WHERE  商品.商品名称  ='" + Sname + "'";

                SqlCommand sqlcom2 = new SqlCommand(sqlstr2, sqlcon);
                //得到商品编号
                SqlDataReader data = sqlcom2.ExecuteReader();
                try
                {

                    while (data.Read())
                    {

                        string sno = data["商品编号"].ToString();

                        //客户编号
                        string user_id = Session["id"].ToString();
                        //创建时间
                        string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        //数量

                        string num = GridView1.Rows[i].Cells[3].Text;
                        int num1 = int.Parse(num);
                        //单价
                        string price = GridView1.Rows[i].Cells[4].Text;
                        float price1 = float.Parse(price);
                        float ALLprice = num1 * price1;
                        string Allprice1 = ALLprice.ToString();


                        //订单状态
                        string finish = "0";
                        string finish1 = "0";
                        string sqlstr = "insert into 订单(客户编号,商品编号,数量,总价,创建时间,订单状态,收货状态) values('" + user_id + "','" + sno + "','" + num + "','" + Allprice1 + "','" + time + "','" + finish + "','"+finish1+"')";

                        SqlCommand sqlcom3 = new SqlCommand(sqlstr, con);
                        sqlcom3.ExecuteNonQuery();

                    }

                    data.Close();
                }
                catch (SqlException ex)
                {
                    Response.Write(ex.Message.ToString());
                }
                finally
                {

                    data.Close();

                }

            }
        }

    }

    //当多选框被选中时发生的方法
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        int count = 0;
        int num1 = 0;
        float price1 = 0;
        float ALLprice = 0;
        float Allprice1 = 0;
        int cc = 3;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            string num = GridView1.Rows[i].Cells[3].Text;
            num1 = int.Parse(num);
            //单价
            string price = GridView1.Rows[i].Cells[4].Text;
            price1 = float.Parse(price);
            ALLprice = (num1 * price1);
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                Allprice1 = Allprice1 + ALLprice;
                count = count + 1;
                Label3.Visible = true;
            }
        }
        String Allprice3 = Allprice1.ToString();
        String count2 = count.ToString();
        Label5.Text = count2;
        Label5.Visible = true;
        Label3.Text = Allprice3;
        Label3.Visible = true;
    }

    //全选按钮
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        int count = 0;
        int num1 = 0;
        float price1 = 0;
        float ALLprice = 0;
        float Allprice1 = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            string num = GridView1.Rows[i].Cells[3].Text;
            num1 = int.Parse(num);
            //单价
            string price = GridView1.Rows[i].Cells[4].Text;
            price1 = float.Parse(price);
            ALLprice = (num1 * price1);
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
                if (cbox.Checked == true)
                {
                    Allprice1 = Allprice1 + ALLprice;
                    count = count + 1;
                    Label3.Visible = true;
                }

            }
            else
            {
                cbox.Checked = false;
                Label3.Text = "0";
                Label5.Text = "0";

            }

        }
        String Allprice3 = Allprice1.ToString();
        String count2 = count.ToString();
        Label5.Text = count2;
        Label5.Visible = true;
        Label3.Text = Allprice3;
        Label3.Visible = true;
    }
    //查找
    protected void Button3_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        Label3.Text = "0";
        Label5.Text = "0";
        Button4.Visible = false;
        SqlConnection conn = new SqlConnection(strCon);
        //SqlCommand sqlcom;
        string Sname = TextBox1.Text;
        int count = 0;
        int num1 = 0;
        float price1 = 0;
        float ALLprice = 0;
        float Allprice1 = 0;
        if (TextBox1.Text == "")
        {

            GridView1.DataSourceID = "SqlDataSource1";
            GridView1.DataBind();

        }
        else
        { //打开数据库
            conn.Open();
            string sqlstr = "select 选购.编号, 商品.商品名称, 商品.图片, 选购.数量, 商品.价格 from 选购,商品,客户 where 商品.商品编号= 选购.商品编号 and 客户.客户编号 = 选购.客户编号 and 商品.商品名称 like'%" + Sname + "%'";
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
            //实例化数据集DataSet
            DataSet ds = new DataSet();
            da.Fill(ds);
            //绑定DataList控件
            GridView1.DataSource = ds;//设置数据源，用于填充控件中的项的值列表
            GridView1.DataSourceID = "";
            GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
        }


        if (GridView1.Rows.Count == 0)
        {
            GridView1_empty();
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;
            Label7.Visible = false;
            CheckBox2.Visible = false;
            Button1.Visible = false;
            Button2.Visible = false;
        }
        else
        {
            CheckBox2.Visible = true;
            Button1.Visible = true;
            Button2.Visible = true;
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            Label5.Visible = true;
            Label6.Visible = true;
            Label7.Visible = true;

        }
        conn.Close();

    }

    //结算
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        Label3.Text = "0";
        Label5.Text = "0";
        int num1;
        float Allprice2 = 0;
        float price1 = 0;
        float ALLprice = 0;
        string Allprice3 = "";
        CheckBox cbox = null;
        int count = 0;
        sqlcon = new SqlConnection(strCon);
        SqlConnection con3 = new SqlConnection(strCon);

        SqlConnection con2 = new SqlConnection(strCon);
        SqlCommand sqlcom;
        dingdanAdd();
        con2.Open();
        con3.Open();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            string num = GridView1.Rows[i].Cells[3].Text;
            num1 = int.Parse(num);
            //单价
            string price = GridView1.Rows[i].Cells[4].Text;
            price1 = float.Parse(price);
            ALLprice = (num1 * price1);
            cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");


            if (cbox.Checked == true)
            {
                //得到商品表的总数量
                string sqlstr3 = "select 商品.数量,商品.商品编号 from 商品,选购 where 商品.商品编号 = 选购.商品编号";
                //得到选购表的数量

                SqlCommand sqlcom2 = new SqlCommand(sqlstr3, con3);
                //得到商品编号
                SqlDataReader data1 = sqlcom2.ExecuteReader();
                try
                {

                    while (data1.Read())
                    {
                        string no = data1["数量"].ToString();
                        string sno1 = data1["商品编号"].ToString();
                        int no1 = int.Parse(no);
                        int no2 = no1 - num1;
                        string no4 = no2.ToString();
                        string sqlstr5 = " update 商品 Set 数量 = '" + no4 + "'where 商品.商品编号 = '" + sno1 + "'";
                        SqlCommand sqlcom4 = new SqlCommand(sqlstr5, con2);

                        sqlcom4.ExecuteNonQuery();

                        count = count + 1;
                        //消费的总价格
                        Allprice2 = Allprice2 + ALLprice;
                        sqlcon.Open();
                        string sqlstr = "delete from 选购 where 编号='" + GridView1.DataKeys[i].Value + "'";
                        sqlcom = new SqlCommand(sqlstr, sqlcon);
                        sqlcom.ExecuteNonQuery();
                        sqlcon.Close();

                    }
                    data1.Close();



                }
                catch (SqlException ex)
                {
                    Response.Write(ex.Message.ToString());
                }
                finally
                {

                    data1.Close();



                }
            }

        }

        GridView1.DataBind();
        if (GridView1.Rows.Count == 0)
        {
            GridView1_empty();
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;
            Label7.Visible = false;
            CheckBox2.Visible = false;
            Button1.Visible = false;
            Button2.Visible = false;
        }
        else
        {
            CheckBox2.Visible = true;
            Button1.Visible = true;
            Button2.Visible = true;
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            Label5.Visible = true;
            Label6.Visible = true;
            Label7.Visible = true;

        }
    }
    //编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        CheckBox2.Checked = false;
        GridView1.EditIndex = e.NewEditIndex;
        int id = GridView1.EditIndex;
        //单价
        string num = GridView1.Rows[id].Cells[3].Text;
        int num1 = int.Parse(num);
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                string price = GridView1.Rows[id].Cells[4].Text;
                float price1 = float.Parse(price);
                //得到修改之前的商品的总价格
                float ALLprice = (num1 * price1);
                Label8.Text = ALLprice.ToString();
                //这里写你绑定GridView的方法比如GridViewBind();
                GridView1.DataBind();
            }

        }

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        CheckBox2.Checked = false;

        SqlConnection con = new SqlConnection(strCon);
        con.Open();
        var row = GridView1.Rows[e.RowIndex];
        // 提取Id字段的值
        GridView1.Columns[3].ExtractValuesFromCell(
        e.Keys,
        row.Cells[3] as DataControlFieldCell,
        DataControlRowState.Edit,
        true /* includereadonly */ );
        string num = (string)e.NewValues["数量"];
        int num1 = int.Parse(num);


        try
        {
            if (num1 < 1)
            {
                int num2 = 1;
                string sqlstr5 = " update 选购 Set 数量 = '" + num2 + "'  where  编号 = '" + GridView1.DataKeys[e.RowIndex].Value + "'";

                SqlCommand sqlcom4 = new SqlCommand(sqlstr5, con);
                sqlcom4.ExecuteNonQuery();
                sqlcom4.Dispose();
                con.Close();

            }
            else
            {
                string sqlstr6 = " update 选购 Set 数量 = '" + num1 + "'  where  编号 = '" + GridView1.DataKeys[e.RowIndex].Value + "'";

                SqlCommand sqlcom5 = new SqlCommand(sqlstr6, con);
                sqlcom5.ExecuteNonQuery();
                sqlcom5.Dispose();
                con.Close();

            }

        }
        catch (Exception ex)
        {
            Response.Write("数据库错误，错误原因：" + ex.Message);
            Response.End();
        }




        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                int id = GridView1.EditIndex;
                //单价
                string price = GridView1.Rows[id].Cells[4].Text;
                float price1 = float.Parse(price);
                //得到修改后商品的总价格
                float ALLprice = (num1 * price1);

                //原来商品的价格
                float Oldprice = float.Parse(Label8.Text);
                float xianjia = float.Parse(Label3.Text);
                Label3.Text = (xianjia - Oldprice + ALLprice).ToString();
                Label3.Visible = true;
            }
            else
            {
                Label3.Text = "0";
                Label5.Text = "0";
            }

        }



    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        Label3.Text = "0";
        CheckBox2.Checked = false;
        Label5.Text = "0";
        GridView1.DataBind();
        if (GridView1.Rows.Count == 1)
        {
            Button4.Visible = true;
            Button2.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;
            Label7.Visible = false;
            CheckBox2.Visible = false;
            Button1.Visible = false;

        }

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Button4.Visible = false;
        if (GridView1.Rows.Count == 0)
        {
            GridView1_empty();
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;
            Label7.Visible = false;
            CheckBox2.Visible = false;
            Button1.Visible = false;
            Button2.Visible = false;
        }
        else
        {
            CheckBox2.Visible = true;
            Button1.Visible = true;
            Button2.Visible = true;
            Label2.Visible = true;
            Label3.Visible = true;
            Label4.Visible = true;
            Label5.Visible = true;
            Label6.Visible = true;
            Label7.Visible = true;

        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;

        }
        Label5.Text = "0";
        Label5.Visible = true;
        Label3.Text = "0";
        Label3.Visible = true;
    }
    protected void GridView1_empty()
    {
        Button4.Visible = false;
        DataTable dt = new DataTable();
        dt.Columns.Add("编号");
        dt.Columns.Add("商品编号");
        dt.Columns.Add("客户编号");
        dt.Columns.Add("数量");
        dt.Columns.Add("价格");
        dt.Columns.Add("商品名称");
        dt.Columns.Add("图片");
        if (dt.Rows.Count == 0)
        {
            dt.Rows.Add(dt.NewRow());
        }
        GridView1.DataSourceID = "";
        GridView1.DataSource = dt;
        GridView1.DataBind();
        int columnCount = dt.Columns.Count;
        GridView1.Rows[0].Cells.Clear();
        GridView1.Rows[0].Cells.Add(new TableCell());
        GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
        GridView1.Rows[0].Cells[0].Text = "您的购物车空空如也，快去买买买吧！";
        GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
    }

}
